package com.sky.common.config;

import com.sky.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类, 用于配置OSS相关的bean
 *
 * @author toki*/
@Configuration
@Slf4j
@ConditionalOnClass(AliOssUtil.class) // 仅在类路径中存在AliOssUtil类时, 才会实例化该类
public class OssConfiguration {

    @Bean
    @ConditionalOnMissingBean // 若容器中不存在AliOssUtil实例, 则创建该实例(工具类一个就够)
    public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){
        log.info("初始化阿里云OSS文件上传工具类: {}", aliOssProperties);
        return new AliOssUtil(aliOssProperties.getEndpoint(),
                aliOssProperties.getAccessKeyId(),
                aliOssProperties.getAccessKeySecret(),
                aliOssProperties.getBucketName());
    }
}
